<?php
/**
 * Created by PhpStorm.
 * User: boye009
 * Date: 2016/3/22
 * Time: 15:49
 */

namespace Bbs\Api;

use Bbs\Model\ForumPostLikeModel;
use Common\Api\Api;

class ForumPostLikeApi extends Api{

    /**
     * 添加
     */
    const ADD = "Bbs/ForumPostLike/add";
    /**
     * 保存
     */
    const SAVE = "Bbs/ForumPostLike/save";
    /**
     * 保存根据ID主键
     */
    const SAVE_BY_ID = "Bbs/ForumPostLike/saveByID";

    /**
     * 删除
     */
    const DELETE = "Bbs/ForumPostLike/delete";

    /**
     * 查询
     */
    const QUERY = "Bbs/ForumPostLike/query";
    /**
     * 查询一条数据
     */
    const GET_INFO = "Bbs/ForumPostLike/getInfo";

    /**
     * 插入多条数据
     */
    const ADD_ALL = "Bbs/ForumPostLike/addAll";

    /**
     * 无分页查询
     */
    const QUERY_NO_PAGING = "Bbs/ForumPostLike/queryNoPaging";

    /**
     * 统计
     */
    const COUNT = "Bbs/ForumPostLike/count";

    /**
     * 点赞我的
     */
    const QUERY_WITH_USER = "Bbs/ForumPostLike/queryWithUser";

    protected function _init(){
        $this -> model = new ForumPostLikeModel();
    }

    public function queryWithUser($map = null, $page = ['curpage'=>1,'size'=>10], $order = 'like_time desc', $params = false, $fields = false){

        $model = $this->model;

        $query = $model->alias('p')->join('__FORUM_THREAD__ as t on t.tid=p.tid')
            ->join('common_member as m on m.uid=p.uid');

        if(!is_null($map)){
            $query = $query->where($map);
        }
        if($order!==false){
            $query = $query->order($order);
        }
        $list = $query->page($page['curpage'] . ',' . $page['size'])
            ->select();

        $query = $this->model->alias('p')->join('__FORUM_THREAD__ as t on t.tid=p.tid')
            ->join('common_member as m on m.uid=p.uid');
        if(!is_null($map)){
            $query = $query->where($map);
        }

        $count = $query->count();
        if($list === false){
            $error = $this->model->getDbError();
            return $this->apiReturnErr($error);
        }else{
            return $this->apiReturnSuc(['count'=>$count,'list'=>$list]);
        }

    }


}